package com.engine.modules.vector.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.exception.InvalidTokenRuntimeException;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.modules.vector.mapper.VectorInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@Api(tags = "3-矢量表属性接口")
@ApiSort(value = 3)
@CrossOrigin
@RequestMapping("/Vector/Info")
@Slf4j
public class VectorInfoController {

    @Autowired
    VectorInfoMapper vectorInfoMapper;

    @ApiOperation(value = "获取矢量的style", notes = "获取矢量的style")
    @GetMapping(value = "/selectGroupStyle/{tableName}/{token}")
    @ApiOperationSupport(order = 1)
    public String selectTableStyle(@PathVariable("tableName") String tableName,
                                   @PathVariable("token") String token,
                                   HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            Map<String, Object> rsMap = vectorInfoMapper.selectTableStyle(tableName);
            if (rsMap != null) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsMap, 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "获取矢量组的style", notes = "获取矢量组的style")
    @GetMapping(value = "/selectGroupStyle/{groupName}/{token}")
    @ApiOperationSupport(order = 2)
    public String selectGroupStyle(@PathVariable("groupName") String groupName,
                                   @PathVariable("token") String token,
                                   HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            Map<String, Object> rsMap = vectorInfoMapper.selectGroupStyle(groupName);
            if (rsMap != null) {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", rsMap, 1, response);
                return jsonResponseEntity.getJsonString();
            } else {
                JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_RESULT_NULL, "无结果", response);
                return jsonResponseEntity.getJsonString();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "获取表信息", notes = "通过表名查询表信息，会返回json格式的表信息")
    @GetMapping(value = {"/selectTableInfo/{tableName}/{token}"}, produces = "text/html;charset=UTF-8")
    @ApiOperationSupport(order = 3)
    @ResponseBody
    public String selectTableInfo(@PathVariable("tableName") String tableName,
                                  @PathVariable(value = "token",required = false) String token,
                                  HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            Map<String, Object> map = vectorInfoMapper.selectTableInfo(tableName);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "查询成功", map, 0, response);
            return jsonResponseEntity.getJsonString(SerializerFeature.WriteNullStringAsEmpty);
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
}
